package com.by.handler;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

//@ControllerAdvice//声明为响应视图
//@ResponseBody//响应为json
@RestControllerAdvice//为以上两个注解的混合注解
public class GlobalExceptionHandler {
    @ExceptionHandler(NullPointerException.class)
    public Map handler(NullPointerException e){
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", e.toString());
        return map;
    }
}
